<template>
  <div>
    <div class="margin-container-list">
      <el-tabs v-model="selectStatus" @tab-click="selectStatusClick">
        <el-tab-pane label="已选商品" name="1"></el-tab-pane>
        <el-tab-pane label="未选商品" name="0"></el-tab-pane>
      </el-tabs>
      <!--顶部搜索 -->
      <div class="top-operate">
        <div>
          <div class="float-l">
            <el-select class="select-class" v-model="textCol" style="width: 120px">
              <el-option label="商品名称" value="title"></el-option>
              <el-option label="商品货号" value="goodsCode"></el-option>
            </el-select>
            <el-input @keyup.enter.native="handleSearch" v-model="searchKeyword" placeholder="请输入关键词" style="width: 200px"></el-input>
            <el-cascader v-model="categoryIds"  placeholder="选择商品分类" :props="{ checkStrictly: true }" change-on-select :options="categoryData"  class="default-input" filterable></el-cascader>
            <el-button type="primary" plain @click="handleSearch">查询</el-button>
            <el-button plain="" @click="resetSearch">重置</el-button>
          </div>
          <div class="btn-search">
            <br>
            <el-button v-if="selectStatus == 0" @click="handleBatchSelect" type="primary">批量添加至已选</el-button>
            <el-button v-if="selectStatus == 1" @click="handleBatchRemove" type="primary">批量移除</el-button>
          </div>
        </div>
      </div>

      <!-- 内容列表 -->
      <div class="table-container">
        <el-table  ref="multipleTable" :data="goodsList" v-loading="listLoading" @selection-change="handleSelectionChange"
                   element-loading-text="加载中" style="width: 100%" >
          <el-table-column width="50px" type="selection"></el-table-column>
          <el-table-column min-width="320px" label="商品名称" prop="title">
            <template slot-scope="scope">
              <div class="flex v-align-c cursor-p">
                <label><img :src="scope.row.mainImage" class="user-avatar"></label>
                <div class="font-14 pad-l-10">
                  <div>{{scope.row.title}}</div>
                  <div>
                    <span class="font-gray">商品货号：{{scope.row.goodsCode}}</span>
                  </div>
                </div>
              </div>
            </template>>
          </el-table-column>
          <el-table-column width="100px" label="上下架状态" prop="goodsStatus">
            <template slot-scope="scope">
              {{dict.goodsStatus[scope.row.goodsStatus]}}
            </template>
          </el-table-column>
          <el-table-column width="120px" label="零售价格(元)" prop="sellPrice"></el-table-column>
          <el-table-column width="100px" label="库存" prop="stock">
            <template slot-scope="scope">
              {{scope.row.stock - (scope.row.stockInUse||0)}}
            </template>
          </el-table-column>
          <el-table-column width="150px" label="操作">
            <template slot-scope="scope">
              <el-button v-if="selectStatus == 1" type="primary" @click="removeGoodsId(scope.row.goodsId)">移除</el-button>
              <el-button v-if="selectStatus == 0" type="primary" @click="addGoodsId(scope.row.goodsId)">添加至已选</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 列表分页 -->
      <div class="pagination">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                       :current-page="searchListQuery.currentPage"
                       :page-sizes="[10, 20, 50, 100]" :page-size="searchListQuery.size"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total">
        </el-pagination>
      </div>
    </div>
    <div class="dialog-footer mar-b-20">
      <el-button type="primary" @click="saveGoodsIds">确定</el-button>
    </div>
  </div>
</template>

<style lang="less" scoped>
  .user-avatar{
    width: 68px;
    height: 68px;
    display: block;
    padding: 1px;
    border: #efefef 1px solid;
  }
  .category-style{
    color: #3abbdb
  }
</style>

<script>
  import { pageSelectGoods, listGoodsCategory } from '@/api/goods/goods'
  import { getDictByGroupName } from '@/api/common/common'
  import elDragDialog from '@/directive/el-dragDialog'

  export default {
    components: {},
    directives: {
      // 拖动窗口
      elDragDialog
    },
    props: {
      goodsIds: {
        type: Array,
        default() {
          return []
        }
      }
    },
    data() {
      return {
        total: null,
        multipleSelection: [],
        listLoading: true,
        goodsSortNo: null,
        searchListQuery: {
          enterpriseId: this.getEnterpriseId(),
          currentPage: 1,
          size: 10,
          title: '',
          goodsStatus: '',
          goodsTypes: '30,32'
        },
        textCol: 'title',
        searchKeyword: '',
        dict: {
          goodsStatus: {}
        },
        categoryIds: [],
        goodsList: [],
        // 商品的所有分类json
        categoryData: [],
        selectStatus: '1',
        pageSelection: {}
      }
    },
    // 初始化页面
    created() {
      this.initDict()
      this.listGoodsCategory()
      this.initGoodsList()

      const that = this
      this.$bus.$on('setGoodsIds', function(goodsIds) {
        const temp = Array.from(goodsIds)
        that.goodsIds.splice(0, that.goodsIds.length)
        temp.forEach(goodsId => {
          that.goodsIds.push(goodsId)
        })
        that.initGoodsList()
      })
    },
    filters: {
    },
    mounted() {
    },
    methods: {
      // 更改当前页数
      handleCurrentChange(val) {
        this.searchListQuery.currentPage = val
        this.initGoodsList()
      },
      // 更改当前页面大小
      handleSizeChange(val) {
        this.searchListQuery.size = val
        this.initGoodsList()
      },
      // 勾选
      handleSelectionChange(items) {
        if (items.length === 0) {
          this.pageSelection[this.searchListQuery.currentPage] = []
        } else {
          this.pageSelection[this.searchListQuery.currentPage] = items.map(item => item.goodsId)
        }
      },
      selectStatusClick() {
        this.resetSearch()
      },
      initDict() {
        getDictByGroupName('ProjectGoodsDTO').then(response => {
          this.dict = response.data
        })
      },
      initGoodsList() {
        this.searchListQuery = {
          enterpriseId: this.getEnterpriseId(),
          currentPage: this.searchListQuery.currentPage,
          size: this.searchListQuery.size,
          title: '',
          goodsStatus: '',
          goodsTypes: '30,32'
        }
        this.searchListQuery[this.textCol] = this.searchKeyword
        this.searchListQuery.selectStatus = this.selectStatus
        this.searchListQuery.goodsIds = this.goodsIds.join(',')
        if (this.categoryIds.length > 0) {
          this.searchListQuery.oneCategoryId = this.categoryIds[0]
        }
        if (this.categoryIds.length > 1) {
          this.searchListQuery.twoCategoryId = this.categoryIds[1]
        }
        if (this.categoryIds.length > 2) {
          this.searchListQuery.threeCategoryId = this.categoryIds[2]
        }
        this.listLoading = true
        // 搜索状态
        pageSelectGoods(this.searchListQuery).then(response => {
          this.total = response.data.total
          this.goodsList = response.data.records
          Object.assign(this.dict, response.data.dict)
          this.listLoading = false
        })
      },
      // 获取类目
      listGoodsCategory() {
        listGoodsCategory(this.getEnterpriseId()).then(response => {
          this.categoryData = response.data
        })
      },
      handleSearch() {
        this.initGoodsList()
      },
      resetSearch() {
        this.textCol = 'title'
        this.searchKeyword = ''
        this.categoryIds = []
        this.searchListQuery.currentPage = 1
        this.searchListQuery.size = 10
        this.initGoodsList()
      },
      // 移除商品
      removeGoodsId(goodsId) {
        this.goodsIds.splice(this.goodsIds.findIndex(e => e === goodsId), 1)
        this.initGoodsList()
      },
      // 选择商品
      addGoodsId(goodsId) {
        this.goodsIds.push(goodsId)
        this.initGoodsList()
      },
      // 批量移除商品
      handleBatchRemove() {
        const that = this
        const goodsIds = this.pageSelection[this.searchListQuery.currentPage]
        goodsIds.forEach(goodsId => that.goodsIds.splice(that.goodsIds.findIndex(e => e === goodsId), 1))
        this.initGoodsList()
      },
      // 批量选择商品
      handleBatchSelect() {
        const that = this
        const goodsIds = this.pageSelection[this.searchListQuery.currentPage]
        goodsIds.forEach(goodsId => that.goodsIds.push(goodsId))
        this.initGoodsList()
      },
      // 确定选择商品
      saveGoodsIds() {
        this.$emit('saveGoodsIds', this.goodsIds)
        this.selectStatus = '1'
        this.resetSearch()
      }
    }
  }

</script>
